package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.DamageListDao;
import com.atguigu.jxc.dao.DamageListGoodsDao;
import com.atguigu.jxc.entity.DamageList;
import com.atguigu.jxc.entity.DamageListGoods;
import com.atguigu.jxc.service.DamageListGoodsService;
import com.atguigu.jxc.vo.DamageSaveGoodsVo;
import com.atguigu.jxc.vo.TestDamageSaveGoodsVo;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author ONIE
 * @create 2021/11/26 2:14 上午
 */
@Service
public class DamageListGoodsServiceImpl implements DamageListGoodsService {
	@Autowired
	private DamageListGoodsDao damageListGoodsDao;
	@Autowired
	private DamageListDao damageListDao;

	@Override
	public void saveDamage(String damageNumber, DamageSaveGoodsVo damageSaveGoodsVo) {
		String damageListGoodsStr = damageSaveGoodsVo.getDamageListGoodsStr();
		Gson gson = new Gson();
		JsonArray jsonElements = gson.fromJson(damageListGoodsStr, JsonArray.class);
		List<DamageListGoods> damageListGoodsList = new ArrayList<>();
		DamageList damageList = new DamageList();
		damageList.setDamageDate(damageSaveGoodsVo.getDamageDate());
		damageList.setRemarks(damageSaveGoodsVo.getRemarks());
		damageList.setDamageNumber(damageNumber);
		damageListDao.saveDamageList(damageList);
		System.out.println(damageList.getDamageListId());
		for (JsonElement jsonElement : jsonElements) {
			DamageListGoods damageListGoods = gson.fromJson(jsonElement, DamageListGoods.class);
			damageListGoods.setDamageListId(damageList.getDamageListId());
			damageListGoodsList.add(damageListGoods);
		}
		damageListGoodsDao.saveDamageListGoods(damageListGoodsList);

	}

	@Override
	public List<DamageList> listDamage(String sTime, String eTime) {
		return damageListDao.listDamage(sTime,eTime);
	}

	@Override
	public List<DamageListGoods> goodsList(Integer damageListId) {
		return damageListGoodsDao.goodsList(damageListId);
	}
}

